const fs = require('fs');

const tours = JSON.parse(
    fs.readFileSync(`${__dirname}/../dev-data/data/tours-simple.json`)
);
exports.chickId = (req,res,next,val)=>{
    console.log(val,'抓取ID')
    if (req.params.id*1 > tours.length) {
        return res.status(404).json({
            status: "fail",
            message: 'Invalid Id'
        })
    }
    next();
}
exports.checkBody = (req,res,next)=>{
    if(!req.body.name || !req.body.price){
        return res.status(400).json({
            status:'fail',
            message:"missing name or price"
        })
    }
}
exports.getAllTour = (req, res) => {
    console.log(req.requestTime);
    res.status(200).json({
        status: 'success',
        requestTime: req.requestTime,
        results: tours.length,
        data: {
            tours
        }
    })
}
exports.getTour = (req, res) => {
    console.log(req.params)
    const id = req.params.id * 1;
    
    const tour = tours.find(el => el.id === id)

    res.status(200).json({
        status: 'success',
        data: {
            tour
        }
    })
}
exports.createTour = (req, res) => {
    console.log(req.body);
    const newId = tours[tours.length - 1].id + 1;
    const newTour = Object.assign({
        id: newId
    }, req.body);
    tours.push(newTour);
    fs.writeFile(`${__dirname}/dev-data/data/tours-simple.json`, JSON.stringify(tours), err => {
        res.status(201).json({
            status: "success",
            data: {
                tour: newTour
            }
        })
    })
    // res.send('Done'); 发送请求后无法设置请求头
}
exports.updateTour = (req, res) => {
  res.status(200).json({
        status: 'success',
        data: {
            tour: '<Updated tour here....>'
        }
    })
}
exports.deleteTour = (req, res) => {
    res.status(204).json({
        status: 'success',
        data: null
    })
}